﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MShop.Web.Components;
using MShop.Core.Domain;

namespace MShop.Web.Areas.Admin.Controllers
{
    public class DepartmentController : SecureController
    {
        //
        // GET: /Department/

        public ActionResult Index()
        {
           // var departments = DataContext.Department;
          //  BuildDepartmentTree();
            return View();
        }

        private void InitDepartmentListItems(object selectedValue)
        {
           // ViewData["departments"] =  DataAccessUtil.GetSortDepartments(true);         
        }



       
 
        public ActionResult Edit(int? id)
        {
            Department dept = new Department();
            if (id.HasValue)
            {
              //  dept = DataContext.Department.First(x => x.Id == id.Value);
               // InitDepartmentListItems(dept.ParentId);
            }
            else
            {
                InitDepartmentListItems(id);
            }
            
            return View(dept);
        }

        //
        // POST: /Department/Edit/5

        [HttpPost]
        public ActionResult Edit(int? id,Department department, FormCollection collection)
        {
            //if (ModelState.IsValid)
            //{
            //    Department dept = null;
            //    if (id.HasValue)
            //    {
            //        dept = DataContext.Department.First(x => x.Id == id.Value);
            //        dept.ParentId = department.ParentId;
            //        dept.DeptName = department.DeptName;
            //        dept.DeptMemo = department.DeptMemo;
            //        DataContext.SaveChanges();
            //        return RedirectToAction("Index");
            //    }
            //    else
            //    {
            //        DataContext.AddToDepartment(department);
            //        DataContext.SaveChanges();
            //        return RedirectToAction("Index");
            //    }
            //}
            //else
            //{
            //    InitDepartmentListItems(department.ParentId);
            //    return View(department);
            //}
            return Content("a");
        }

        //
        // GET: /Department/Delete/5
 
        public ActionResult Delete(int id)
        {
            //var department= DataContext.Department.First(x => x.Id == id);
            //DataContext.DeleteObject(department);

            //DataContext.SaveChanges();

            return RedirectToAction("Index");
           
        }

        //
        // POST: /Department/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
           
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
